mui.init();



mui.plusReady(function(){
	var name = plus.storage.getItem(KeyCfg.userName);
	$("#name").val(name);
	var tran_card = CmwMcore.$('tran_card');
	MeventMgr.onTouchstart(tran_card,function(event,self){
		var a= document.getElementById("a");
		a.checked=true;
		$(".tranpwd").show();
		$(".emailpwd").hide();
		$(".resetpwd").hide();
	});
	
	var email_card = CmwMcore.$('email_card');
	MeventMgr.onTouchstart(email_card,function(event,self){
		var b= document.getElementById("b");
		b.checked=true;
		$(".tranpwd").hide();
		$(".emailpwd").show();
		$(".resetpwd").hide();
	});
	
	/**------- 身份证+原始密码验证   -------*/
	var btn_tranpwd = CmwMcore.$('btn_tranpwd');
	MeventMgr.onTouchstart(btn_tranpwd,function(event,self){
		if(!valid()) return;
		submitForm();
	});
	
	/**------- 邮箱验证码+身份证  -------*/
	var btn_emailpwd = CmwMcore.$('btn_emailpwd');
	MeventMgr.onTouchstart(btn_emailpwd,function(event,self){
		if(!valid1()) return;
		submitForm1();
	});
	
	/**------- 提交新密码  -------*/
	var btn_resetpwd = CmwMcore.$('btn_resetpwd');
	MeventMgr.onTouchstart(btn_resetpwd,function(event,self){
		submitForm2();
	});
	
	
	function submitForm(){
		var card = $("#card").val().trim();
		var bpassword =$("#bpassword").val().trim();
		var url =UrlConfig.checkPasswordCard;
		var pars={card:card,bpassword:bpassword};
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
                	mui.alert("验证成功");
                	$(".qbox").hide();
                    $(".tranpwd").hide();
					$(".emailpwd").hide();
					$(".resetpwd").show();
                } else if (status == "errorCard") {
                	mui.alert("身份证验证错误");                	
                } else if (status == "errorPassword") {
                	mui.alert("原始密码错误");            	
                } else {
                	mui.alert("验证失败");               	
                }
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		});
	}
	
	
	function submitForm1(){
		var card = $("#card1").val().trim();
		var emailcode =$("#emailcode").val();
		var url =UrlConfig.checkEmailCard;
		var pars={card:card,emailcode:emailcode};
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
                	mui.alert("验证成功");
                	$(".qbox").hide();
                    $(".tranpwd").hide();
					$(".emailpwd").hide();
					$(".resetpwd").show();
                } else if (status == "errorCard") {
                	mui.alert("身份证号码错误");                	
                } else if (status == "errorEmail") {
                	mui.alert("邮箱验证失败");            	
                } else {
                	mui.alert("验证失败");               	
                }
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		});
	}
	
	function valid(){
		var cardid_val = $("#card").val().trim();
		var tranpwd_val =$("#bpassword").val().trim();
		
		if(!cardid_val || !cardid_val.trim()){
			mui.alert("请输入您的身份证号!");
			return false;
		}
		if(!tranpwd_val || !tranpwd_val.trim()){
			mui.alert("请输入原始密码!");
			return false;
		}
		var statuss = false;
		card = cardid_val.trim();
		if (card.length == 15 || card.length == 18) {
			//中国身份证 15位或者18位
			statuss = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(card);
		} else if (/(^[A-Z]$)/.test(card.substr(0, 1))) {
			//香港身份证以字母开头，只验证是否以字母开头，不验证长度
			statuss = true;
		} else if (card.length == 8) {
			//港澳身份证
			statuss = /(^[A-Z0-9]\d{6}[A-Z0-9]$)/.test(card);
		}
		if(!statuss){
			mui.alert("身份证号码格式错误!");
			return false; 
		}
		return true;
	}
	
	function valid1(){
		var cardid_val = $("#card1").val().trim();
		var emailcode_val =$("#emailcode").val();
		
		if(!cardid_val || !cardid_val.trim()){
			mui.alert("请输入您的身份证号!");
			return false;
		}
		
		if(!emailcode_val || !emailcode_val.trim()){
			mui.alert("请输入验证码!");
			return false;
		}
		
		var statuss = false;
		card = cardid_val.trim();
		if (card.length == 15 || card.length == 18) {
			//中国身份证 15位或者18位
			statuss = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(card);
		} else if (/(^[A-Z]$)/.test(card.substr(0, 1))) {
			//香港身份证以字母开头，只验证是否以字母开头，不验证长度
			statuss = true;
		} else if (card.length == 8) {
			//港澳身份证
			statuss = /(^[A-Z0-9]\d{6}[A-Z0-9]$)/.test(card);
		}
		if(!statuss){
			mui.alert("身份证号码格式错误!");
			return false; 
		}
		return true;
	}
	
	
	
	 //提交修改密码
    function submitForm2() {
        var pwd2 = $("#newpwd").val().trim();
        var pwd3 = $("#tranpwd").val().trim();
        
    	var regshuzi=/^[\d]+$/;
    	var regzimu = /^[A-Za-z]+$/;
        var status = /^(?=[0-9a-zA-Z]|[a-zA-Z0-9]+\d)(?=[0-9a-zA-Z]|[a-zA-Z0-9]+[a-zA-Z])(?=[0-9a-zA-Z]|[a-zA-Z0-9]).{8,16}$/.test(pwd2);
       
        if (!pwd3.match(regshuzi) && !pwd3.match(regzimu) && status) {
            if (pwd2 == pwd3) {
            	var pars ={param:pwd3,msg:1};
            	var url= UrlConfig.ajaxtranPassword;
            	MuiEventMgr.ajax(url,{pars:pars,
				  	sfn : function(data){
				  		var status = data.status;
				  		var info = data.info;
				  		if (status == "y") {
		                	var urls =UrlConfig.updatepassword;
		                	var pars ={newpassword:pwd3};
		                	MuiEventMgr.ajax(urls,{pars:pars,
				  				sfn : function(data){
		                			var status= data.status;
		                			var info = data.info;
		                			 if (status == "true") {
                                     	mui.alert("修改成功");
                                     	mui.back();
	                                 } else {
	                                	mui.alert(info);
	                                 }
		                		
		                		},ffn : function(err){
				  					mui.alert("获取数据失败：["+err+"]");
				  				}
		                	});

		                } else {
		                	mui.alert(info);               	
		                }
			        		
				  	},ffn : function(err){
				  		mui.alert("获取数据失败：["+err+"]");
				  	}
				});
            } else {
            	mui.alert("新密码和确认密码不一致!");           	
            }
        } else {
        	mui.alert("两个密码都要是8-16位字母加数字的组合！");
        }


    }
});



	

